package com.ruoyi.answer.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.answer.domain.ExamPaper;
import com.ruoyi.answer.domain.ExamPaperQuestion;
import com.ruoyi.answer.domain.ExamQuestionChoice;
import com.ruoyi.answer.domain.vo.ExamPaperQuestionVo;
import com.ruoyi.answer.domain.vo.ExamQuestionChoiceVo;
import com.ruoyi.answer.mapper.ExamPaperQuestionMapper;
import com.ruoyi.answer.mapper.ExamQuestionChoiceMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.answer.domain.bo.ExamCompositionBo;
import com.ruoyi.answer.domain.vo.ExamCompositionVo;
import com.ruoyi.answer.domain.ExamComposition;
import com.ruoyi.answer.mapper.ExamCompositionMapper;
import com.ruoyi.answer.service.IExamCompositionService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 试卷和试题关联组成Service业务层处理
 *
 * @author kun_er
 * @date 2022-08-16
 */
@RequiredArgsConstructor
@Service
public class ExamCompositionServiceImpl implements IExamCompositionService {

    private final ExamCompositionMapper baseMapper;
    private final ExamPaperQuestionMapper examPaperQuestionMapper;
    private final ExamQuestionChoiceMapper examQuestionChoiceMapper;

    /**
     * 查询试卷和试题关联组成
     */
    @Override
    public ExamCompositionVo queryById(Long id){
        ExamCompositionVo examCompositionVo = baseMapper.selectVoById(id);
        ExamPaperQuestion examPaperQuestion = examPaperQuestionMapper.selectById(examCompositionVo.getQuestionId());
        BeanUtils.copyProperties(examPaperQuestion,examCompositionVo);
        return examCompositionVo;
    }

    /**
     * 查询试卷和试题关联组成列表
     */
    @Transactional
    @Override
    public TableDataInfo<ExamCompositionVo> queryPageList(ExamCompositionBo bo, PageQuery pageQuery) {
        QueryWrapper<ExamComposition> wrapper = new QueryWrapper<>();
        wrapper.eq("paper_id",bo.getPaperId());

        wrapper.orderByAsc("question_order");
        Page<ExamCompositionVo> result = baseMapper.selectVoPage(pageQuery.build(), wrapper);
        List<ExamCompositionVo> records = result.getRecords();
        List<ExamCompositionVo> collect = records.stream().map(item -> {
            ExamPaperQuestionVo examPaperQuestionVo = examPaperQuestionMapper.selectVoById(item.getQuestionId());
            List<ExamQuestionChoiceVo> list = examQuestionChoiceMapper.selectVoList(new QueryWrapper<ExamQuestionChoice>().eq("question_id", item.getQuestionId()));
            item.setExamQuestionChoiceVoList(list);
            BeanUtils.copyProperties(examPaperQuestionVo,item);

            return item;
        }).collect(Collectors.toList());
        return TableDataInfo.build(collect,result.getTotal());
    }

    /**
     * 查询试卷和试题关联组成列表
     */
    @Override
    public List<ExamCompositionVo> queryList(ExamCompositionBo bo) {
        LambdaQueryWrapper<ExamComposition> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamComposition> buildQueryWrapper(ExamCompositionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamComposition> lqw = Wrappers.lambdaQuery();

        return lqw;
    }

    /**
     * 新增试卷和试题关联组成
     */
    @Transactional
    @Override
    public Boolean insertByBo(ExamCompositionBo bo) {
        ExamComposition add = BeanUtil.toBean(bo, ExamComposition.class);
        ExamPaperQuestion examPaperQuestion = BeanUtil.toBean(bo, ExamPaperQuestion.class);

        ExamPaperQuestion getId = validEntityBeforeSave(examPaperQuestion);
        add.setQuestionId(getId.getId());
        boolean flag = baseMapper.insert(add) > 0;

        return flag;
    }

    /**
     * 修改试卷和试题关联组成
     */
    @Transactional
    @Override
    public Boolean updateByBo(ExamCompositionBo bo) {
        ExamComposition update = BeanUtil.toBean(bo, ExamComposition.class);
        ExamPaperQuestion examPaperQuestion = BeanUtil.toBean(bo, ExamPaperQuestion.class);
        boolean b = baseMapper.updateById(update) > 0;
        boolean b1 = examPaperQuestionMapper.updateById(examPaperQuestion) > 0;
        if (b&&b1){
            return true;
        }else {
            return false;
        }
//        validEntityBeforeSave(update);
    }

    /**
     * 保存前的数据校验
     */
    private ExamPaperQuestion validEntityBeforeSave(ExamPaperQuestion entity){

        int insert = examPaperQuestionMapper.insert(entity);
        if (insert>0){
            return entity;
        }else {
            return null;
        }
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试卷和试题关联组成
     */
    @Transactional
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            // 做一些业务上的校验,判断是否需要校验

            List<Long> collect = ids.stream().map(id -> {
                ExamComposition examComposition = baseMapper.selectById(id);
                return examComposition.getQuestionId();
            }).collect(Collectors.toList());
            if (collect.size()>0){
                int i = examPaperQuestionMapper.deleteBatchIds(collect);
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
